// import { get } from "./request";
// import { isEmpty } from "./data";
import { getSessionItem } from "@/utils/sessionStorage";
import { getH4a } from "@/api/home/h4a";
/**
 * 登录-重定向
 */
export function login(successFunc, failedFunc) {
  //后端地址
  loginUrl()
    .then((url) => openloginWindow(url, successFunc, failedFunc))
    .catch((err) => failedFunc(err));
}

function openloginWindow(url, successFunc, failedFunc) {
    console.log(url);
    // window.location.href=url
  let iWidth = 650;
  let iHeight = 550;
  let iTop =
    (window.screenY || window.screenTop || 0) + (screen.height - iHeight) / 2;
  let iLeft =
    (window.screenX || window.screenLeft || 0) + (screen.width - iWidth) / 2;
  let loginWin = window.open(
    url,
    "H4A登录",
    "height=" +
      iHeight +
      ",width=" +
      iWidth +
      ",left=" +
      iLeft +
      ",top=" +
      iTop +
      ",toolbar=0, menubar=0, scrollbars=auto, resizable=0, location=0, status=0"
  );
  loginWin.focus();
  window.addEventListener(
    "message",
    function (e) {

      console.log(e);
      try {
        if (e.data.token) {
          successFunc(e.data.token);
        }
      } catch (error) {
        failedFunc(error);
      }
    },
    false
  );
}

/**
 * 登录-获取H4a的URL自己调用，不通过后端重定向
 */
export function loginUrl() {
  return new Promise((resolve, reject) => {
    // let url=encodeURIComponent(location.href)
    let loginUrl =
      "/login-url/login-url?redirect=" + location.origin + location.pathname;
   getH4a( loginUrl)
      .then((res) => {

        console.log(res);
        resolve(res.data);
      })
      .catch((err) => {
        console.log("获取H4A-登录Url失败", err);
        reject(err);
      });
  });
}

/** 登出 **/
export function logout(token, callBackFunc) {
//   if (getSessionItem(token)) token = "";
  // let url =
  //   process.env.VUE_APP_BASE_API +
  //   "/logOut/logOut?token=" +
  //   token +
  //   "&redirect=" +
  //   location.origin;
  
    // OutH4a(url);
  // window.open(url, "_self");
//   callBackFunc();
}
